package com.wanggsx.library.base.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MyCancelableRadioButton extends androidx.appcompat.widget.AppCompatRadioButton implements View.OnClickListener {
    private OnClickListener mClickListener;
    private OnCheckedChangeListener checkedChangeListener;

    @Override
    public void setOnClickListener(OnClickListener l) {
        mClickListener = l;
        super.setOnClickListener(this);
    }

    public MyCancelableRadioButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        super.setOnClickListener(this);
    }

    private int mLastCheckedId = 0;
    private boolean mIsLastChecked = false;
    @Override
    final public void onClick(View v) {
        if (v instanceof RadioButton){
            if (mIsLastChecked && mLastCheckedId ==
                    v.getId()){
                mLastCheckedId = 0;
                mIsLastChecked = false;
                if (v.getParent() instanceof RadioGroup){
                    ((RadioGroup)v.getParent()).clearCheck();
                }
            }else{
                mLastCheckedId = v.getId();
                mIsLastChecked = true;
            }
        }
        if (mClickListener!=null){
            mClickListener.onClick(this);
        }
    }

//    @Override
//   final public void setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener listener) {
//
//    }
//
//    @Override
//    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//        mIsLastChecked = isChecked;
//        if (checkedChangeListener!=null){
//            checkedChangeListener.onCheckedChanged(buttonView,isChecked);
//        }
//    }
}
